package test;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserInfoMapper {
   public int  update(UserInfo userInfo){
        try( Connection connection=DBUtil.getConnection();) {
          String sql="UPDATE userinfo " +
                      "SET password=?,nickname=?,age=? "+
                       "WHERE username=?";
          PreparedStatement ps=connection.prepareStatement(sql);
          ps.setString(1, userInfo.getPassword());
          ps.setString(2, userInfo.getNickname());
          ps.setInt(3,userInfo.getAge());
            ps.setString(4, userInfo.getUsername());
              return  ps.executeUpdate();

       } catch (SQLException e) {
           e.printStackTrace();
       }
        return 0;
    }
    public int insert(UserInfo userInfo){
        try(Connection connection =DBUtil.getConnection();) {
            String sql="INSERT INTO userinfo(username,password,nickname,age) "+
                    "VALUES(?,?,?,?)";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1, userInfo.getUsername());
            ps.setString(2, userInfo.getPassword());
            ps.setString(3,userInfo.getNickname());
            ps.setInt(4,userInfo.getAge());
            return ps.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    public UserInfo selectByUsername(String username) {
        try(Connection connection=DBUtil.getConnection();) {
            String sql="SELECT username,password,nickname,age "+
                       "FROM userinfo "+
                       "WHERE username=?";
            PreparedStatement ps=connection.prepareStatement(sql);
            ps.setString(1,username);
            ResultSet rs =ps.executeQuery();
            if(rs.next()){
                UserInfo userInfo=new UserInfo();
                userInfo.setUsername(rs.getString("username"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setNickname(rs.getString("nickname"));
                userInfo.setAge(rs.getInt("age"));
                return userInfo;
            }
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return null;
    }

    public List<UserInfo> selectAll(){
        List<UserInfo> list = new ArrayList<>();
        try (Connection connection = DBUtil.getConnection();){
            Statement statement = connection.createStatement();
            String sql = "SELECT username,password,nickname,age " +
                    "FROM userinfo";
            ResultSet rs = statement.executeQuery(sql);
            while(rs.next()){
                UserInfo userInfo = new UserInfo();
                userInfo.setUsername(rs.getString("username"));
                userInfo.setPassword(rs.getString("password"));
                userInfo.setNickname(rs.getString("nickname"));
                userInfo.setAge(rs.getInt("age"));
                list.add(userInfo);}
        } catch (SQLException e) {
            e.printStackTrace();
        }
                return list;
    }
}
